using Game.Component.Character;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;

namespace Game.Component
{
	public class ToothToolItem : HadRopeItem
	{
		private ToothMachineDisplayerItem displayer;

		private CharacterItem targetItem;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		public override void ResetState()
		{
			base.ResetState();
			InitPlaceItem();
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			UpdatePutBackAnim();
		}

		protected override void OnItemCompTriggerEnter(ColliderData collData)
		{
			base.OnItemCompTriggerEnter(collData);
			if (collData.trigger.type != ItemComponentTrigger.Type.Mouth || !typeof(CharacterItem).IsAssignableFrom(collData.trigger.owner.GetType()))
			{
				return;
			}
			ResetTarget(false);
			targetItem = (CharacterItem)collData.trigger.owner;
			((CharacterItem)collData.trigger.owner).SetMouthState(Face.MouthState.Open);
			if (displayer == null)
			{
				BaseItem itemByScriptType = Singleton<Scene>.Instance.GetItemByScriptType(114);
				if (itemByScriptType != null)
				{
					displayer = (ToothMachineDisplayerItem)itemByScriptType;
				}
			}
			if (displayer != null)
			{
				displayer.AddToothTool(this);
			}
		}

		protected override void PlayDragSound()
		{
		}

		protected override void OnItemCompTriggerExit(ColliderData collData)
		{
			base.OnItemCompTriggerExit(collData);
			if (targetItem != null && collData.trigger.type == ItemComponentTrigger.Type.Mouth && collData.trigger.owner == targetItem)
			{
				ResetTarget(true);
			}
		}

		private void ResetTarget(bool stopDisplayer)
		{
			if (targetItem != null)
			{
				targetItem.SetMouthState(Face.MouthState.Normal);
				targetItem = null;
			}
			if (!stopDisplayer)
			{
				return;
			}
			if (displayer == null)
			{
				BaseItem itemByScriptType = Singleton<Scene>.Instance.GetItemByScriptType(114);
				if (itemByScriptType != null)
				{
					displayer = (ToothMachineDisplayerItem)itemByScriptType;
				}
			}
			if (displayer != null)
			{
				displayer.RemoveToothTool(this);
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			needBack = true;
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			PutBackPlaceItem();
			ResetTarget(true);
		}
	}
}
